package com.tripcube.ai.controller;

import com.tripcube.ai.model.dto.BaseChatDTO;
import com.tripcube.ai.model.vo.BaseChatVO;
import com.tripcube.ai.service.BaseChatService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

/**
 * 基础聊天控制器
 * 
 * @author alander
 */
@Tag(name = "基础聊天", description = "AI聊天基础功能接口")
@RestController
@RequestMapping("/ai/baseChat")
@Slf4j
@RequiredArgsConstructor
public class BaseChatController {

    private final BaseChatService baseChatService;

    /**
     * 简单聊天接口
     * 
     * @param request 聊天请求参数
     * @return 聊天响应结果
     */
    @Operation(summary = "简单聊天", description = "发送消息并获取AI回复")
    @PostMapping("/send")
    public BaseChatVO simpleChat(
            @Parameter(description = "聊天请求参数", required = true)
            @RequestBody BaseChatDTO request) {
        log.info("收到简单聊天请求: {}", request.getQuery());
        return baseChatService.simpleChat(request);
    }

    /**
     * 流式聊天接口
     * 
     * @param request 聊天请求参数
     * @param response HTTP响应对象
     * @return 流式聊天响应
     */
    @Operation(summary = "流式聊天", description = "发送消息并以流式方式获取AI回复")
    @PostMapping("/stream")
    public Flux<String> streamChat(
            @Parameter(description = "聊天请求参数", required = true)
            @RequestBody BaseChatDTO request,
            HttpServletResponse response) {
        log.info("收到流式聊天请求: {}", request.getQuery());
        response.setCharacterEncoding("UTF-8");
        return baseChatService.streamChat(request);
    }

    /**
     * 带对话记忆的聊天接口
     * 
     * @param id 会话ID，用于标识对话上下文
     * @param request 聊天请求参数
     * @param response HTTP响应对象
     * @return 流式聊天响应
     */
    @Operation(summary = "记忆聊天", description = "基于会话ID进行具有上下文记忆的AI对话")
    @PostMapping("/advisor/{id}")
    public Flux<String> advisorChat(
            @Parameter(description = "会话ID", required = true)
            @PathVariable String id,
            @Parameter(description = "聊天请求参数", required = true)
            @RequestBody BaseChatDTO request,
            HttpServletResponse response) {
        log.info("收到记忆聊天请求: 会话ID={}, 消息={}", id, request.getQuery());
        response.setCharacterEncoding("UTF-8");
        request.setConversationId(id);
        return baseChatService.advisorChat(request);
    }
}
